class MessageManager {
  constructor(options) {
    const optionNames = Object.getOwnPropertyNames(options);
    optionNames.forEach((name) => {
      this[name] = options[name];
    });
  }

  waitHandleMsgList = [];
  cacheList = [];
  actioningMsgList = [];
  isHandling = false;
//   isError = false;
  interalFlag = null;

  push = (message) => {
    console.log("推入数据");
    this.cacheList.push(message);
    const remainder = this.waitHandleMsgList.length % this.onceHandleLength;
    if (remainder + this.cacheList.length >= this.onceHandleLength) {
      const spliceLength = this.onceHandleLength - remainder;
      this.waitHandleMsgList.push(...this.cacheList.splice(0, spliceLength));
    }
    if (this.cacheList.length === 0) {
      if (this.interalFlag !== null) {
        clearTimeout(this.interalFlag);
      }
    }
    if (this.cacheList.length === 1) {
      this.setIntervalAction();
    }
    this.handleAction();
  };

  setIntervalAction = () => {
    this.interalFlag = setTimeout(() => {
      this.waitHandleMsgList.push(...this.cacheList);
      const cacheListLength = this.cacheList.length
      this.cacheList = [];
      console.log("计时器执行了, 添加至waitHandleMsgList " + cacheListLength);
      clearTimeout(this.interalFlag);
      this.interalFlag = null;
      this.handleAction();
    }, this.interval);
  };

  handleAction = () => {
    if (this.isError || this.isHandling) {
      return;
    }

    if (this.waitHandleMsgList.length === 0) {
      return;
    }

    if (this.actioningMsgList.length === 0) {
    //   console.log("waitHandleMsgList", this.waitHandleMsgList);
      this.actioningMsgList = this.waitHandleMsgList.splice(
        0,
        this.onceHandleLength
      );
    //   console.log("waitHandleMsgList", this.waitHandleMsgList);
    }

    //遇到错误后，重发是否带上新的数据
    // else {
    //     const addList = this.waitHandleMsgList.splice(0, this.onceHandleLength - this.actioningMsgList.length)
    //     this.actioningMsgList = [...this.actioningMsgList, ...this.addList]
    // }

    this.isHandling = true;
    console.log("发起请求！", this.actioningMsgList.length);
    new Promise((resolve, reject) =>
      this.action(this.actioningMsgList, resolve, reject)
    ).then(this.handleResolve, this.handleRejected)
    .catch((e)=>{
        //抛出异常，acion执行异常
        throw new Error('请检查action参数')
    });
  };

  handleResolve = (result) => {
    this.isHandling = false;
    this.actioningMsgList = [];
    // console.log('cacheList',this.cacheList);
    // console.log(this.waitHandleMsgList);
    console.log('成功')
    if (this.waitHandleMsgList.length) {
      console.log("成功接着发");
      this.handleAction();
    }
  };

  handleRejected = (reason) => {
    console.log("失败重发");
    this.isHandling = false;
    this.handleAction();
  };

  callback = () => {};

  errorHandle = (response, actioningMsgList, waitHandleMsgList) => {};

  log = (logContent) => {};

  action = () => {};
}

export default class MsgManagerBuilder {
  static init = (options) => {
    //可以做options校验，参数属性是否符合要求
    const msgManager = new MessageManager(options);
    return msgManager;
  };
}
